package com.googlecode.mailnet.emf.netmodel.ui;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;

import com.googlecode.mailnet.emf.netmodel.NetworkConfigurationModel;
import com.googlecode.mailnet.emf.netmodel.diagram.edit.parts.NetworkConfigurationModelEditPart;
import com.googlecode.mailnet.emf.netmodel.diagram.part.NetmodelDiagramEditor;
import com.googlecode.mailnet.emf.netmodel.impl.NetworkConfigurationModelImpl;
import com.googlecode.mailnet.emf.netmodel.simulation.DecorationUtils;
import com.googlecode.mailnet.emf.netmodel.simulation.InvalidModelException;
import com.googlecode.mailnet.emf.netmodel.simulation.SimulationContext;
import com.googlecode.mailnet.math.distribution.DistributionFormatException;

public class SimulationRunHandler extends AbstractHandler implements IHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
		
		IEditorPart editorPart = window.getActivePage().getActiveEditor();
		if (! (editorPart instanceof NetmodelDiagramEditor)) return null;
		NetmodelDiagramEditor editor = (NetmodelDiagramEditor) editorPart;
		DiagramEditPart diagramEditPart = editor.getDiagramEditPart();
		NetworkConfigurationModelEditPart modelEditPart = (NetworkConfigurationModelEditPart) diagramEditPart;
		
		try {
			Diagram diagram = (Diagram) modelEditPart.getModel();
			if (diagram.getElement() == null) return null;
			if (!(diagram.getElement() instanceof NetworkConfigurationModel)) {
				System.out.println("ProcessModelDefaultAction: diagram.getElement() is not a NetworkConfiguration (" + diagram.getElement().getClass().toString() +")");
				return null;
			}
			NetworkConfigurationModel model = (NetworkConfigurationModel) diagram.getElement();
			SimulationContext context = model.getSimulationContext();
			if (context == null) {
				context = new SimulationContext();	
			}
			context.start(model);
			
			if (model.getDecoratorId() == null) {
				((NetworkConfigurationModelImpl) model).setDecoratorId(DecorationUtils.DECORATOR_FAILURES);
			}
			modelEditPart.onDecoratorUpdated();
	
			// notify view about the new SimulationContext object
			IViewPart viewPart = window.getActivePage().findView("com.googlecode.mailnet.emf.diagram.ui.SimulationView");
			if (viewPart == null || !(viewPart instanceof SimulationView)) return null;
			SimulationView simulationView = (SimulationView) viewPart;
			simulationView.onModelChanged(model);
			
			// window.getSelectionService().
		} catch (InvalidModelException e) {
			MessageDialog.openError(window.getShell(), "Model error", e.getMessage());
		} catch (DistributionFormatException e) {
			MessageDialog.openError(window.getShell(), "Distribution format error", e.getMessage());
		} catch (IllegalStateException e) {
			MessageDialog.openError(window.getShell(), "Simulation failure", e.getMessage());
		} catch (Exception e) {
			MessageDialog.openError(window.getShell(), "Unexpected exception", e.toString());
		}
		
		return null;
	}

}
